package com.mer.sevice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mer.dao.HeadNavDao;
import com.mer.entity.HeadNav;
import com.mer.sevice.HeadNavService;
import com.mer.vo.CommonResult;
import com.mer.vo.HeadNavVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @program: HeadNavServiceImpl
 * @description:
 * @author: 吴昊
 * @create: 2021/10/19 20:25
 **/
@Service
public class HeadNavServiceImpl implements HeadNavService {
    @Resource
    private HeadNavDao headNavDao;
    @Override
    public CommonResult getAllHead(Integer page, Integer limit, HeadNavVo headNavVo) {
        Page<HeadNav> headNavPage = new Page<>(page,limit);
        QueryWrapper<HeadNav> headNavQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(headNavVo.getHeadName())){
            headNavQueryWrapper.like("head_name",headNavVo.getHeadName());
        }
        if (StringUtils.isNotEmpty(headNavVo.getIsDiable())){
            headNavQueryWrapper.eq("is_diable",headNavVo.getIsDiable());
        }
        Page<HeadNav> headNavPage1 = headNavDao.selectPage(headNavPage, headNavQueryWrapper);
        if (headNavPage.getSize()>0){
            return new CommonResult(2000,"查询成功",headNavPage1);
        }else {
            return new CommonResult(5000,"查询失败",null);
        }
    }

    @Override
    public CommonResult delete(Integer headId) {
        HeadNav headNav = new HeadNav();
        headNav.setHeadId(headId);
        headNav.setIsDiable("1");
        int i = headNavDao.deleteById(headNav);
        if (i>0){
            return new CommonResult(2000,"删除成功",null);

        }else {
            return new CommonResult(5000,"删除失败",null);
        }

    }

    @Override
    public CommonResult status(Integer headId, String isDiable) {
        HeadNav headNav = new HeadNav();
        headNav.setHeadId(headId);
        headNav.setIsDiable(isDiable);
        int i = headNavDao.updateById(headNav);
        if (i>0){
            return new CommonResult(2000,"修改成功",null);

        }else {
            return new CommonResult(5000,"修改失败",null);
        }

    }

    @Override
    public CommonResult add(HeadNavVo headNavVo) {
        HeadNav headNav = new HeadNav();
        if (headNavVo.getHeadId()!=null){
            headNav.setHeadId(headNavVo.getHeadId());
            headNav.setHeadName(headNavVo.getHeadName());
            headNav.setHref(headNavVo.getHref());
            int i = headNavDao.updateById(headNav);
            if (i>0){
                return new CommonResult(2000,"修改成功",null);

            }else {
                return new CommonResult(5000,"修改失败",null);
            }
        }else {
            headNav.setHeadId(headNavVo.getHeadId());
            headNav.setHeadName(headNavVo.getHeadName());
            headNav.setHref(headNavVo.getHref());
            headNav.setIsDiable("0");
            headNav.setIsDelete("0");
            int insert = headNavDao.insert(headNav);
            if (insert>0){
                return new CommonResult(2000,"添加成功",null);

            }else {
                return new CommonResult(5000,"添加失败",null);
            }
        }

    }
}
